package com.zyd.service.base.impl;

import com.zyd.io.page.SearchKey;
import com.zyd.io.page.OperateKey;

import com.zyd.io.page.PageBean;
import com.zyd.io.page.PageRequestBean;
import com.google.common.collect.Maps;
import com.zyd.io.vo.page.MultiQueryPageVO;
import com.zyd.service.base.BaseService;
import com.zyd.util.SqlUtils;
import com.zyd.util.StringUtils;
import com.zyd.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

@Component
public class BaseServiceImpl implements BaseService {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     *
     * @return
     * @param
     */
    @Override
    public Map<String,Object> getBase(String id) {
        String sql="select networkname,networkcode  from zydzn_network where dr =0 and id=?";
        Map<String, Object> data = jdbcTemplate.queryForMap(sql, id);
        return  data;

    }

    /**
     * @param
     * @param
     * @return
     */
    @Override
    public PageBean<Map<String, Object>> getBaselist(MultiQueryPageVO request) {
        String countSql = "select count(1) from ";
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM zydzn_network a  WHERE a.dr=0 ORDER BY a.ts DESC ");
        sql.append(SqlUtils.addMultiCondition(request.getSearchKeys()));

        Long totalRows = jdbcTemplate.queryForObject(countSql + "(" + sql.toString() + ")as a", Long.class);
        String pageSql = SqlUtils.addPage(sql.toString(), request.getCurrentPage(), request.getPageSize());

        List<Map<String, Object>> ret = jdbcTemplate.query(pageSql, new RowMapper<Map<String, Object>>() {

            @Override
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                Map<String, Object> ret = Maps.newHashMap();
                ret.put("id", rs.getString("id"));
                ret.put("networkname", rs.getString("networkname"));
                ret.put("networkcode", rs.getString("networkcode"));

                return ret;
            }
        });
        PageBean<Map<String, Object>> pageBean = new PageBean<Map<String, Object>>(request.getCurrentPage(), request.getPageSize(), totalRows.intValue());
        pageBean.setItems(ret);
        return pageBean;


    }

    @Override
    public PageBean<Map<String, Object>> getBasePage(MultiQueryPageVO request) {
        String countSql = "select count(*)";
        String selectFields = "select * ";
        StringBuilder sql = new StringBuilder();
        sql.append(" FROM zydzn_network a  WHERE a.dr=0 ");
        sql.append(disposeSql(request.getSearchKeys()));

        System.out.println(sql);
        Long totalRows = jdbcTemplate.queryForObject(countSql + sql.toString(), Long.class);
        String pageSql = SqlUtils.addPage(selectFields + sql.toString()+"ORDER BY a.ts DESC", request.getCurrentPage(), request.getPageSize());

        List<Map<String, Object>> maps = jdbcTemplate.queryForList(pageSql);
        PageBean<Map<String, Object>> pageBean = new PageBean<>(request.getCurrentPage(), request.getPageSize(), totalRows.intValue());
        pageBean.setItems(maps);
        return pageBean;


    }

    public void add(Map<String, Object> map) {
        String networkname = (String) map.get("networkname");
        String networkcode = (String) map.get("networkcode");
        jdbcTemplate.update("INSERT INTO zydzn_network(id,networkname,networkcode,ts,dr) values (?,?,?,?,?)", UUIDUtil.getUUid(), networkname, networkcode, new Date(), 0);
    }

    public void update(Map<String, Object> map) {
        String id = (String) map.get("id");
        String networkname = (String) map.get("networkname");
        String networkcode = (String) map.get("networkcode");
        jdbcTemplate.update("UPDATE zydzn_network SET networkname=?,networkcode=? where id = ?", networkname, networkcode, id);
    }


    public void delete(Map<String, Object> map) {
        String id = (String) map.get("id");
        jdbcTemplate.update("update zydzn_network set dr = 1 where id = ?", id);
    }

    //    处理sql
    public String disposeSql(List<SearchKey> searchKeys) {
        StringBuilder sql = new StringBuilder();
        for (SearchKey searchKey : searchKeys) {
            sql.append("  and a. " + searchKey.getName() + " " + searchKey.getOperate() + " ");
            if (searchKey.getOperate().equalsIgnoreCase(OperateKey.LIKE)) {
                sql.append("\'" + SqlUtils.andLike(searchKey.getValue()) + "\'");
            } else {
                //支持 =、>、<、!=、like、in、notin
                sql.append("\'" + searchKey.getValue() + "\'");
            }
        }
        return sql.toString();
    }

}

